import {defineStore} from 'pinia'
import { deviceStore } from './deviceStore'


const state = () => ({
	index: 1, //自增ID
	list: []
})

const getters = {

}

const actions = {
	getList() {
		return JSON.parse(JSON.stringify(this.list));
	},
	getBoxList(){
		const device = deviceStore();
		let arr = []
		for(let i=1;i<=device.data.number;i++){
			arr.push({
				id: i,
				code: i < 10 ? '0' + i : i,
				goods:this.list.filter(item => item.boxId == i)
			})
		}
		// console.log(arr)
		return arr;
	},
	save(data) { 
		
	
		if(!data.id){
			//新增
			data.id = this.index;
			this.index++;
			this.list.push(data);
		}else{
			//修改
			this.list = this.list.map(item => {
				if (item.id == data.id) {
					return data;
				}
				return item;
			})
		}
		
	},
	del(id) {
		this.list = this.list.filter(item => item.id != id);
	},

}

export const goodsStore = defineStore({
	id: 'goods-store',
	state,
	getters,
	actions,
	persist: { //  持久化插件
		enabled: true, // 使用存储 
	}

})